/**
 * 
 */
package com.javaking.clanteam.studentutils.courses;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TimePicker;
import android.widget.ToggleButton;

import com.actionbarsherlock.app.SherlockDialogFragment;
import com.javaking.clanteam.studentutils.R;
import com.javaking.clanteam.studentutils.utils.DatePair;
import com.javaking.clanteam.studentutils.utils.ListAdder;
import com.javaking.clanteam.studentutils.utils.OnItemsChangedListener;

/**
 * @author scott
 *
 */
public class NewTimeDialog extends SherlockDialogFragment implements ListAdder<DatePair> {
	
	OnItemsChangedListener<DatePair> mListener;
	
	/**
	 * @see android.app.DialogFragment#onCreateDialog(android.os.Bundle)
	 */
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		builder.setTitle("New Time:");
		View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_addtime, null);
		
		
		// TODO Add period support again.
//		CheckBox checkBox = (CheckBox)view.findViewById(R.id.timingOption);
//		if (checkBox.isChecked()) {
//			view.findViewById(R.id.collegeTiming).setVisibility(View.VISIBLE);
//			view.findViewById(R.id.periodSystem).setVisibility(View.GONE);
//		} else {
//			view.findViewById(R.id.collegeTiming).setVisibility(View.GONE);
//			view.findViewById(R.id.periodSystem).setVisibility(View.VISIBLE);
//		}
//		checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//			
//			@Override
//			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//				Dialog dialog = getDialog();
//				if (dialog == null) 
//					return;
//				
//				if (isChecked) {
//					dialog.findViewById(R.id.collegeTiming).setVisibility(View.VISIBLE);
//					dialog.findViewById(R.id.periodSystem).setVisibility(View.GONE);
//				} else {
//					dialog.findViewById(R.id.collegeTiming).setVisibility(View.GONE);
//					dialog.findViewById(R.id.periodSystem).setVisibility(View.VISIBLE);
//				}
//			}
//		});
		builder.setView(view);
		builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialogInterface, int which) {
				Dialog dialog = getDialog();
//				CheckBox checkBox = (CheckBox)dialog.findViewById(R.id.timingOption);
//				if (checkBox.isChecked()) {
					TimePicker startTimePicker = (TimePicker)dialog
							.findViewById(R.id.startTimePicker);
					
					Calendar startCalendar = Calendar.getInstance();
					startCalendar.set(Calendar.HOUR_OF_DAY,
							startTimePicker.getCurrentHour());
					startCalendar.set(Calendar.MINUTE, startTimePicker.getCurrentMinute());
					
					
					TimePicker endTimePicker = (TimePicker)dialog
							.findViewById(R.id.endTimePicker);
					
					Calendar endCalendar = Calendar.getInstance();
					endCalendar.set(Calendar.HOUR_OF_DAY,
							endTimePicker.getCurrentHour());
					endCalendar.set(Calendar.MINUTE, endTimePicker.getCurrentMinute());
					
					List<DatePair> times = new ArrayList<DatePair>();
					
					if (((ToggleButton)dialog.findViewById(R.id.sunButton)).isChecked()) {
						startCalendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
						endCalendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
						DatePair pair = new DatePair(startCalendar.getTime(), endCalendar.getTime());
						times.add(pair);
					}
					if (((ToggleButton)dialog.findViewById(R.id.monButton)).isChecked()) {
						startCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
						endCalendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
						DatePair pair = new DatePair(startCalendar.getTime(), endCalendar.getTime());
						times.add(pair);
					}
					if (((ToggleButton)dialog.findViewById(R.id.tuesButton)).isChecked()) {
						startCalendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
						endCalendar.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
						DatePair pair = new DatePair(startCalendar.getTime(), endCalendar.getTime());
						times.add(pair);
					}
					if (((ToggleButton)dialog.findViewById(R.id.wedButton)).isChecked()) {
						startCalendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
						endCalendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
						DatePair pair = new DatePair(startCalendar.getTime(), endCalendar.getTime());
						times.add(pair);
					}
					if (((ToggleButton)dialog.findViewById(R.id.thursButton)).isChecked()) {
						startCalendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
						endCalendar.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
						DatePair pair = new DatePair(startCalendar.getTime(), endCalendar.getTime());
						times.add(pair);
					}
					if (((ToggleButton)dialog.findViewById(R.id.friButton)).isChecked()) {
						startCalendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
						endCalendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
						DatePair pair = new DatePair(startCalendar.getTime(), endCalendar.getTime());
						times.add(pair);
					}
					if (((ToggleButton)dialog.findViewById(R.id.satButton)).isChecked()) {
						startCalendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
						endCalendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
						DatePair pair = new DatePair(startCalendar.getTime(), endCalendar.getTime());
						times.add(pair);
					}
					
					mListener.onItemsAdded(times);
					
//				} else {
//					String period =((EditText)dialog.findViewById(R.id.periodNumber)).getText().toString();
//					mListener.onAddTime(period);
//				}
				dialog.dismiss();
			}
		});
		builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		return builder.create();
	}

	@Override
	public void setListener(OnItemsChangedListener<DatePair> listener) {
		mListener = listener;
	}

	@Override
	public OnItemsChangedListener<DatePair> getListener() {
		return mListener;
	}
	

}
